﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Threading;
using System.Windows.Forms;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public class ITXWB
    {
        public string WBEWBELEG = "0";
        string RUNID = "0";
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        BaseFunction INTEXDBC;
        string STKNR = "";

        public ITXWB(string _RUNID)
        {
            RUNID = _RUNID;
            SetUp();
        }
        string ART = "", FARBE = "";
        public void SetUp()
        {
            INTEXDBC = new BaseFunction();
            INTEXDBC.OracleDB = app.OracleDB;
            INTEXDBC.proxy = app.proxy;
            INTEXDBC.IsOnline = app.IsOnline;
            double MTR = 0.0, YD = 0.0;
            Reset(app.TERMNO, "", ref MTR, ref YD);
        }
        public void Reset(string WBBWMPOS, string _STKNR, ref double MTR, ref double YD)
        {
            try
            {
                WBBWMPOS = (app.TERMNO + "  ").Substring(0, 2); 
                OracleCommand or1 = INTEXDBC.OracleDB.CreateCommand(); 
                or1.CommandText = "SELECT ROUND(SUM(WBBISTKLG*0.001)) AS MTR,ROUND(SUM(WBBISTKLG*0.001)/0.9144) AS YD FROM WBBST WHERE WBBART='" + Helpers.Helper.Left(ART.PadRight(20), 20) + "' AND WBBFARBE='" + Helpers.Helper.Left(FARBE.PadRight(10), 10) + "'";
                DataSet SResult = INTEXDBC.Connect2Proxy(or1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    MTR = System.Convert.ToDouble(row["MTR"].ToString());
                    YD = System.Convert.ToDouble(row["MTR"].ToString());
                    //Helper.GetSTime(ref date, ref time, ref stime);
                    //MessageBox.Show(time+"获取长度数据:" + MTR);
                }
            }
            catch (Exception ex)
            {
                //Helper.GetSTime(ref date, ref time, ref stime);
                //MessageBox.Show(time + "发生错误" );
                if (ex.Message.ToLower().Contains("sendtimeout"))
                    INTEXDBC.IsOnline = false;
            }
        }
        public string date = "", time = "", stime = "";
        public void Print(ref double MTR, ref double YD, bool IsReprint = false)
        {
            try
            {
                //Helper.GetSTime(ref date, ref time, ref stime);
                //MessageBox.Show(time+"开始打印任务:");
                MessageInfo msg = new MessageInfo();
                msg.MSGTYPE = "QINEWWB";
                msg.Sender = this.RUNID;
                msg.ACTION = "QINEWWB";
                msg.P1 = app.TERMNO;
                msg.Where = app.User;
                msg.STKNR = STKNR;
                //VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, msg, msgs);
                string _STKNR = QINEWWB(msg, IsReprint);
                Reset(app.TERMNO, _STKNR, ref MTR, ref YD);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        bool IsOnPrint = false;
        public string QINEWWB(MessageInfo msg, bool IsReprint)
        {
            DataSet SResult;
            string _STKNR = "";
            string year = "", date = "", month = "", day = "";
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
            string WBBWMPOS = (msg.P1 + "  ").Substring(0, 2);
            OracleCommand or1 = INTEXDBC.OracleDB.CreateCommand();
            if (!IsReprint)
            {//---
                or1.CommandText = "SELECT P.* FROM WBBST P WHERE  WBBHHKG=0 AND  WBBWAKZ='F'  AND WBBWMPOS='" + WBBWMPOS + "' AND   WBBDRKZ2=' '  AND WBBNEUDAT='" + date + "'";
                //Helper.GetSTime(ref date, ref time, ref stime);
                //MessageBox.Show(time+"非打印查询:" + _STKNR);
            }
            else
            {
                or1.CommandText = "SELECT * FROM (SELECT P.* FROM WBBST P WHERE    WBBWAKZ='F' AND WBBWMPOS='" + WBBWMPOS + "' AND  WBBNEUDAT='" + date + "'  ORDER BY WBBHHKG DESC  )  WHERE ROWNUM<2";
                //Helper.GetSTime(ref date, ref time, ref stime);
                //MessageBox.Show(time+"打印查询:" + _STKNR);
            }
            SResult = INTEXDBC.Connect2Proxy(or1);
            SResult.Tables[0].TableName = "WBBST";
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                _STKNR = row["WBBSTKNR"].ToString();
                break;
            }
            //MessageBox.Show(time+"最后一个布卷是:" + _STKNR);
            /*
            if(!IsOnPrint)
            { 
               
                pr = new Thread(PrintWB);
                pr.IsBackground = true;
                pr.ApartmentState = ApartmentState.STA;
                pr.Start();
            }*/
            SetProd = SResult.Copy();
            PrintWB();
            return _STKNR;

        }
        DataSet SetProd = new DataSet();
        Thread pr;
        ITXEXRPT ITXEXRPT;
        public void PrintWB()
        {
            Helper.GetSTime(ref date, ref time, ref stime);
            //MessageBox.Show(time + "开始打印任务:" );
            IsOnPrint = true;
            string _STKNR = "";
            if (ITXEXRPT == null)
            {
                ITXEXRPT = new WPFExcelReport.ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
            }
            ITXEXRPT.dsSource = new DataSet();
            bool IsFound = false;
            if (SetProd.Tables["WBBST"].Rows.Count == 0)
            {
                //MessageBox.Show(time + "布卷个数为0:");
                IsOnPrint = false;
                return;
            }
       
            switch (app.company)
            {
                default:
                case "JLT":
                    ITXEXRPT.dsSource.Merge(SetProd);
                    ITXEXRPT.DocType = "JLTWBD10IT";
                    foreach (DataRow row in SetProd.Tables["WBBST"].Rows)
                    {
                        string PACKNR = row["WBBPACKNR"].ToString();
                        _STKNR = row["WBBSTKNR"].ToString();
                        Helpers.Helper.GetTime(ref date, ref time);
                        string WBBHHKG = Helpers.Helper.Right("00000000" + time, 8);
                        OracleCommand or1 = INTEXDBC.OracleDB.CreateCommand();
                        string WBBWMPOS = (app.TERMNO + "  ").Substring(0, 2);
                        or1.CommandText = "UPDATE WBBST SET WBBDRKZ2='J',WBBHHKG='" + WBBHHKG 
                            + "' WHERE WBBWMPOS='" + WBBWMPOS + "' AND WBBDRKZ2=' ' AND WBBSTKNR='" + _STKNR + "' ";
                        INTEXDBC.Connect2Proxy(or1);
                        if (PACKNR == "0000")
                            continue;  
                        ART = row["WBBART"].ToString().Trim();
                        FARBE = row["WBBFARBE"].ToString().Trim();
                        string dr = @"C:\Program Files\mabu\templates";
                        string filename = "JLB" + ART.Substring(3, 4) + ".xls";
                        if (!System.IO.Directory.Exists(dr))
                        {
                            IsFound = false;
                        }
                        else
                        {
                            string[] files = System.IO.Directory.GetFiles(dr);
                            foreach (string file in files)
                            {
                                if (!file.Contains(filename)) continue; IsFound = true; ITXEXRPT.strExcelFile = file; break;
                            }
                            if (!IsFound)
                            {
                                filename = "coillabel" + ART.Substring(3, 4) + ".xls";
                                foreach (string file in files)
                                {
                                    if (!file.Contains(filename)) continue; IsFound = true; ITXEXRPT.strExcelFile = file; break;
                                }
                            }
                        }
                    }
                    if (IsFound) ITXEXRPT.WriteExcelReport();
                    break;
            }
           
            //Helper.GetSTime(ref date, ref time, ref stime); 
            //MessageBox.Show(time+"打印完完成:" + _STKNR);
            IsOnPrint = false;
            return;
        }
    }
}
